## This file is part of CDS Invenio.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 CERN.
##
## CDS Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

# pylint: disable-msg=C0103
"""BibEdit Database Layer."""

__revision__ = "$Id$"

from invenio.dbquery import run_sql

def get_name_tags_all():
    """Return a dictionary of all MARC tag's textual names."""
    result = run_sql("SELECT name, value FROM tag")

    # Collect names in a dictionary with field codes as keys.
    nametags = {}
    for el in result:
        nametags[el[1]] = el[0]

    return nametags

def get_bibupload_task_opts(task_ids):
    """Return a list with all set options for list of task IDs TASK_IDS."""
    res = []
    for task_id in task_ids:
        res.append(run_sql("SELECT arguments FROM schTASK WHERE id=%s" %
                           task_id))
    return res

def get_marcxml_of_record_revision(recid, job_date):
    """Return MARCXML string of record revision specified by RECID and JOB_DATE.

    """
    return run_sql("""SELECT marcxml FROM hstRECORD
                       WHERE id_bibrec=%s AND job_date=%s""",
                   (recid, job_date))

def get_record_revisions(recid):
    """Return dates for all known revisions of record RECID."""
    return run_sql("""SELECT id_bibrec,
                             DATE_FORMAT(job_date, '%%Y%%m%%d%%H%%i%%s')
                        FROM hstRECORD WHERE id_bibrec=%s
                    ORDER BY job_date DESC""" % recid)

def get_record_last_modification_date(recid):
    """Return last modification date, as timetuple, of record RECID."""
    return run_sql('SELECT modification_date FROM bibrec WHERE id=%s' %
                   recid)[0][0].timetuple()

def reserve_record_id():
    """Reserve a new record ID in the bibrec table."""
    return run_sql("""INSERT INTO bibrec (creation_date, modification_date)
                       VALUES (NOW(), NOW())""")

def get_related_hp_changesets(recId):
    """
        A function returning the changesets saved in the Holding Pen, related
        to the given record.
    """
    return run_sql("""SELECT changeset_id, changeset_date FROM bibHOLDINGPEN WHERE id_bibrec='%(identifier)s' ORDER BY changeset_date""" % {"identifier" : recId})

def get_hp_update_xml(changeId):
    return run_sql("""SELECT  changeset_xml, id_bibrec from bibHOLDINGPEN where changeset_id=%s""", (str(changeId),))[0]

def delete_hp_change(changeId):
    return run_sql("""delete from bibHOLDINGPEN where changeset_id=%i""" % (changeId, ))
